package gw.com.android.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.gwtsz.gts2.hx.R;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppMain;
import gw.com.android.model.ConfigSettingDeal;
import gw.com.android.model.ConfigType;
import gw.com.android.presenter.AdsData;
import gw.com.android.ui.account.LoginActivity;
import www.com.library.model.DataItemDetail;
import www.com.library.util.DeviceUtil;

/**
 * Created by reeta.zou on 2016/10/8.
 * 广告图片弹层
 */
public class AdsPopWindow extends Dialog {

    protected FragmentActivity mOwnerAct;
    private ImageView mImageView;

    public AdsPopWindow(Activity activity) {
        super(activity, R.style.dialog_loading_bar_no_frame);
        mOwnerAct = (FragmentActivity) activity;
        initParam();
    }

    @Override
    protected void onStart() {
        getWindow().setBackgroundDrawable(new ColorDrawable(0x4D000000));
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    }

    public void setScale() {
        if (mImageView != null) {
            ViewGroup.LayoutParams params = mImageView.getLayoutParams();
            params.width = DeviceUtil.instance().dip2px(345, mOwnerAct);
            params.height = DeviceUtil.instance().dip2px(380, mOwnerAct);
            mImageView.setLayoutParams(params);
        }
    }

    /**
     * 设置图片资源
     */
    public void setImageView(final AdsData.PicData picData) {
        setImageView(picData.title, picData.link, picData.url);
    }

    private void setImageView(final String title, final String link, final String url) {
        if (mOwnerAct instanceof LoginActivity) {//登录页面不弹广告
            return;
        }
        mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(link)) {
                    ConfigSettingDeal mSettingDeal = new ConfigSettingDeal();
                    DataItemDetail item = mSettingDeal.getItemValue(ConfigType.ADS_DETAIL_TAG, title, link);
                    ActivityManager.showWebPageActivity(mOwnerAct, item, AppMain.getAppString(R.string.btn_back));
                }
                dismiss();
            }
        });
        setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Glide.with(mOwnerAct)
                        .load(url)
                        .placeholder(R.mipmap.a_default_adpopup)
                        .error(R.mipmap.a_default_adpopup)
                        .into(mImageView);
            }
        });
    }

    public void initParam() {
        LayoutInflater inflater = getLayoutInflater();
        if (inflater != null) {
            View rootView = inflater.inflate(R.layout.dialog_ads_image, null, false);
            inflaterCustomView(rootView);

            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            /* The next line will add the ProgressBar to the dialog. */
            addContentView(rootView, lp);
        }
    }

    public void inflaterCustomView(View mCustomView) {
        mCustomView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mImageView = (ImageView) mCustomView.findViewById(R.id.ads_imageview);
    }
}
